package zhuhai
{

	import flash.desktop.NativeApplication;
	import flash.events.KeyboardEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;

	[SWF(width="600",height="400",frameRate="12",backgroundColor=0xcccccc)] public class MyKey extends Sprite
	{
		public static var main:MyKey;
		public static const CLICK:String = "click";//单击事件
		public static const DOUBLECLICK:String = "doubleClick";//双击事件
		public static const LEFT:String = "left";//左旋事件
		public static const RIGHT:String = "right";//右旋
		private static var doubleClickEnabled:Boolean = false;//是否可以使用双击

		public function MyKey()
		{
			main = this;
			if(stage)
				stage.addEventListener(KeyboardEvent.KEY_UP, show);
			else
				addEventListener(Event.ADDED_TO_STAGE,init);
		}

		private function init(e:Event):void
		{
			//stage.addEventListener(KeyboardEvent.KEY_UP, show);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, show);
		}

		private var clickTimeoutId:uint;
		private var numClicked:int = 0;
		private function show(e:KeyboardEvent):void
		{
			logs.adds(String.fromCharCode(e.charCode),e.charCode,e.keyCode);
			clearTimeout(clickTimeoutId);
			switch (e.keyCode)
			{
				case Datas.clickkey:
					if(doubleClickEnabled)
					{//可以使用双击
						if(numClicked >0){//触发双击事件
							dispatchEvent(new Event(DOUBLECLICK));
							logs.adds("doubleClick");
							//Toast.show("doubleClick");
							numClicked = 0;
						}else{
							numClicked++;
							clickTimeoutId = setTimeout(resetClick,Datas.doubleClickTimeOut);//超时触发单击事件
						}
					}else{//触发单击事件
						logs.adds("click");
						dispatchEvent(new Event(CLICK));
					}
					break;
				case Datas.leftkey://左旋
					dispatchEvent(new Event(LEFT));
					logs.adds("left");
					//Toast.show("left");
					break;
				case Datas.rightkey://右旋
					dispatchEvent(new Event(RIGHT));
					logs.adds("right");
					//Toast.show("right");
					break;
				case 27://ESCAPE
					e.preventDefault();
					NativeApplication.nativeApplication.exit();
					break;
				default :
					break;
			}
		}

		private function resetClick():void
		{//触发单击事件
			dispatchEvent(new Event(CLICK));
			logs.adds("click");
			//Toast.show("click");
			numClicked = 0;
		}
	}
}
